﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Caliburn.Micro;
using SS.HP.Model;
using Windows.UI.Xaml;

namespace SS.HP.Win8.ViewModels
{
    public class MainViewModel:ViewModelBase
    {
        private string _title;

        public MainViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            _title = "Harry Potter Book Market";
            Books = new Collection<Book>();
            var series = new Series() {Name = "Harry Potter"};
            Books.Add(new Book() {Title="Harry Potter and the Philosopher's Stone", Series = series, SeriesIndex = 1, DiscountedPrice = 8.0, RecommendedRetailPrice = 8.0});
            Books.Add(new Book() { Title = "Harry Potter and the Chamber of Secrets", Series = series, SeriesIndex = 2, DiscountedPrice = 8.0, RecommendedRetailPrice = 8.0 });
            Books.Add(new Book() { Title = "Harry Potter and the Prisoner of Azkaban", Series = series, SeriesIndex = 3, DiscountedPrice = 8.0, RecommendedRetailPrice = 8.0 });
            Books.Add(new Book() { Title = "Harry Potter and the Goblet of Fire", Series = series, SeriesIndex = 4, DiscountedPrice = 8.0, RecommendedRetailPrice = 8.0 });
            Books.Add(new Book() { Title = "Harry Potter and the Order of the Phoenix", Series = series, SeriesIndex = 5, DiscountedPrice = 8.0, RecommendedRetailPrice = 8.0 });

            Cart = new Cart();
        }

        public string Title
        {
            get { return _title; }
            set
            {
                if (value == _title) return;
                _title = value;
                NotifyOfPropertyChange(() => Title);
            }
        }

        public ICollection<Book> Books { get; set; }

        public Cart Cart { get; set; }

        public void BookDropped(DragEventArgs e)
        {
            if (e.Data.Properties.ContainsKey("Book"))
            {
                var book = ((e.Data.Properties["Book"] as Book));
                Cart.AddBook(book.DeepClone());
                NotifyOfPropertyChange(() => CartPrice);
                NotifyOfPropertyChange(() => BooksInCart);
            }
        }

        public double CartPrice
        {
            get { return Cart.Price; }
        }

        public ICollection<Book> BooksInCart
        {
            get { return Cart.Books; }
        }

        public void ClearCart()
        {
            Cart.Clear();
            NotifyOfPropertyChange(() => CartPrice);
            NotifyOfPropertyChange(() => BooksInCart);
        }
    }
}
